package csc.fresher.view;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.EncryptMD5;
import csc.fresher.entity.SystemUser;
import csc.fresher.entity.SystemUserHasRole;
import csc.fresher.service.SystemUserHasRoleService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("request")
public class UpdateSystemUserManageBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String email_pattern = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$";
	private final String phone_pattern = "^\\s*\\d+\\s*$";
	private final String idCardNumber_pattern = "^\\s*\\d+\\s*$";

	private String newPassword;
	private String userName;
	private String firstName;
	private String lastName;
	private String midName;
	private String idCardNumber;
	private String address;
	private String address2;
	private String phone;
	private String phone2;
	private String email;
	private String email2;

	private static Pattern pattern;
	private static Matcher matcher;
	private static String emailStatus;
	private static String phoneStatus;
	private static String emailStatus2;
	private static String phoneStatus2;
	private static String idCardNumberStatus;

	private static String username;
	private static String password;
	private static String checkUniqueIDCardNumber;

	@Autowired
	SystemUserManagementManageBean systemUserManagementManageBean;

	@Autowired
	SystemUserService systemUserService;

	@Autowired
	SystemUserHasRoleService systemUserHasRoleService;

	SystemUser systemUser;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMidName() {
		return midName;
	}

	public void setMidName(String midName) {
		this.midName = midName;
	}

	public String getIdCardNumber() {
		return idCardNumber;
	}

	public void setIdCardNumber(String idCardNumber) {
		this.idCardNumber = idCardNumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress2() {
		return address2;
	}

	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail2() {
		return email2;
	}

	public void setEmail2(String email2) {
		this.email2 = email2;
	}

	public static Pattern getPattern() {
		return pattern;
	}

	public static void setPattern(Pattern pattern) {
		UpdateSystemUserManageBean.pattern = pattern;
	}

	public static Matcher getMatcher() {
		return matcher;
	}

	public static void setMatcher(Matcher matcher) {
		UpdateSystemUserManageBean.matcher = matcher;
	}

	public static String getEmailStatus() {
		return emailStatus;
	}

	public static void setEmailStatus(String emailStatus) {
		UpdateSystemUserManageBean.emailStatus = emailStatus;
	}

	public static String getPhoneStatus() {
		return phoneStatus;
	}

	public static void setPhoneStatus(String phoneStatus) {
		UpdateSystemUserManageBean.phoneStatus = phoneStatus;
	}

	public static String getEmailStatus2() {
		return emailStatus2;
	}

	public static void setEmailStatus2(String emailStatus2) {
		UpdateSystemUserManageBean.emailStatus2 = emailStatus2;
	}

	public static String getPhoneStatus2() {
		return phoneStatus2;
	}

	public static void setPhoneStatus2(String phoneStatus2) {
		UpdateSystemUserManageBean.phoneStatus2 = phoneStatus2;
	}

	public static String getIdCardNumberStatus() {
		return idCardNumberStatus;
	}

	public static void setIdCardNumberStatus(String idCardNumberStatus) {
		UpdateSystemUserManageBean.idCardNumberStatus = idCardNumberStatus;
	}

	public static String getUsername() {
		return username;
	}

	public static void setUsername(String username) {
		UpdateSystemUserManageBean.username = username;
	}

	public static String getPassword() {
		return password;
	}

	public static void setPassword(String password) {
		UpdateSystemUserManageBean.password = password;
	}

	public static String getCheckUniqueIDCardNumber() {
		return checkUniqueIDCardNumber;
	}

	public static void setCheckUniqueIDCardNumber(String checkUniqueIDCardNumber) {
		UpdateSystemUserManageBean.checkUniqueIDCardNumber = checkUniqueIDCardNumber;
	}

	public SystemUserManagementManageBean getSystemUserManagementManageBean() {
		return systemUserManagementManageBean;
	}

	public void setSystemUserManagementManageBean(
			SystemUserManagementManageBean systemUserManagementManageBean) {
		this.systemUserManagementManageBean = systemUserManagementManageBean;
	}

	public SystemUserService getSystemUserService() {
		return systemUserService;
	}

	public void setSystemUserService(SystemUserService systemUserService) {
		this.systemUserService = systemUserService;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public String getEmail_pattern() {
		return email_pattern;
	}

	public String getPhone_pattern() {
		return phone_pattern;
	}

	public String getIdCardNumber_pattern() {
		return idCardNumber_pattern;
	}

	private boolean userAdmin;
	private boolean userSupport;
	private boolean accountAdmin;
	private boolean accountSupport;
	private boolean reportSupport;

	@PostConstruct
	public void init() {
		systemUser = systemUserManagementManageBean.getSystemUser();
		// System.out.println(systemUser.getFirstName());
		userName = systemUser.getUserName();
		firstName = systemUser.getFirstName();
		midName = systemUser.getMidName();
		lastName = systemUser.getLastName();
		idCardNumber = systemUser.getIdCardNumber();
		address = systemUser.getAddress();
		address2 = systemUser.getAddress2();
		phone = systemUser.getPhoneNumber();
		phone2 = systemUser.getPhoneNumber2();
		email = systemUser.getEmail();
		email2 = systemUser.getEmail2();

		List<Long> listRole = systemUserService.getRoleByUserName(userName);
		for (int i = 0; i < listRole.size(); i++) {
			if (listRole.get(i).equals(1L)) {
				userAdmin = true;
			}
			if (listRole.get(i).equals(2L)) {
				userSupport = true;
			}
			if (listRole.get(i).equals(3L)) {
				accountAdmin = true;
			}
			if (listRole.get(i).equals(4L)) {
				accountSupport = true;
			}
			if (listRole.get(i).equals(5L)) {
				reportSupport = true;
			}
		}
		// systemUser = null;
	}

	public List<Long> getAllRole() {
		List<Long> listRole = new ArrayList<Long>();
		if (userAdmin == true)
			listRole.add(1L);
		if (userSupport == true)
			listRole.add(2L);
		if (accountAdmin == true)
			listRole.add(3L);
		if (accountSupport == true)
			listRole.add(4L);
		if (reportSupport == true)
			listRole.add(5L);
		return listRole;
	}

	public String update() {
		SystemUser systemUser = new SystemUser();
		systemUser.setSystemUserID(systemUserManagementManageBean
				.getSystemUser().getSystemUserID());
		systemUser.setUserName(systemUserManagementManageBean.getSystemUser()
				.getUserName());
		if ("".endsWith(newPassword)) {
			systemUser.setPassWord(systemUserManagementManageBean
					.getSystemUser().getPassWord());
		} else {
			systemUser.setPassWord(EncryptMD5.md5(newPassword));
		}
		systemUser.setFirstName(firstName);
		systemUser.setLastName(lastName);
		systemUser.setMidName(midName);
		systemUser.setIdCardNumber(idCardNumber);
		systemUser.setAddress(address);
		systemUser.setAddress2(address2);
		systemUser.setPhoneNumber(phone);
		systemUser.setPhoneNumber2(phone2);
		systemUser.setEmail(email);
		systemUser.setEmail2(email2);
		systemUser.setCreatedDate(systemUserManagementManageBean
				.getSystemUser().getCreatedDate());
		systemUser.setLastUpdate(new Timestamp(System.currentTimeMillis()));
		systemUser.setSystemUserCreateID(systemUserManagementManageBean
				.getSystemUser().getSystemUserCreateID());
		systemUser.setSystemUserActiveID(systemUserManagementManageBean
				.getSystemUser().getSystemUserActiveID());
		systemUserService.updateSystemUser(systemUser);
		
		systemUserManagementManageBean.init();
		return "UserManagement";
	}

	public String updateRole() {
		SystemUserHasRole systemUserHR = new SystemUserHasRole();
		
		systemUserHasRoleService.deletetUserHasRole(systemUserManagementManageBean.getSystemUser().getSystemUserID());
		List<Long> listRole = getAllRole();
		for (int i = 0; i < listRole.size(); i++) {
			systemUserHR.setSystemUserID(systemUserManagementManageBean.getSystemUser().getSystemUserID());
			systemUserHR.setRoleID(listRole.get(i));
			systemUserHasRoleService.insertUserHasRole(systemUserHR);
		}
		systemUserManagementManageBean.init();
		return "UserManagement";
	}

	public boolean isUserAdmin() {
		return userAdmin;
	}

	public void setUserAdmin(boolean userAdmin) {
		this.userAdmin = userAdmin;
	}

	public boolean isUserSupport() {
		return userSupport;
	}

	public void setUserSupport(boolean userSupport) {
		this.userSupport = userSupport;
	}

	public boolean isAccountAdmin() {
		return accountAdmin;
	}

	public void setAccountAdmin(boolean accountAdmin) {
		this.accountAdmin = accountAdmin;
	}

	public boolean isAccountSupport() {
		return accountSupport;
	}

	public void setAccountSupport(boolean accountSupport) {
		this.accountSupport = accountSupport;
	}

	public boolean isReportSupport() {
		return reportSupport;
	}

	public void setReportSupport(boolean reportSupport) {
		this.reportSupport = reportSupport;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
}
